package com.testmanager.testmananger.quantitative.service;

import com.testmanager.testmananger.foundation.util.FlakeIdGenerator;
import com.testmanager.testmananger.quantitative.constants.Sign;
import com.testmanager.testmananger.quantitative.domain.TaskInfoEntity;
import com.testmanager.testmananger.quantitative.domain.WorkerEntity;
import com.testmanager.testmananger.quantitative.dto.TaskInfoDto;
import com.testmanager.testmananger.quantitative.dto.WorkerDto;
import com.testmanager.testmananger.quantitative.mapper.TaskInfoMapper;
import com.testmanager.testmananger.quantitative.mapper.WorkerMapper;
import com.testmanager.testmananger.quantitative.repository.TaskInfoRepository;
import com.testmanager.testmananger.quantitative.repository.WorkerRepository;
import lombok.extern.slf4j.Slf4j;
import org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TaskInfoService {

    @Autowired
    TaskInfoRepository taskInfoRepository;

    @Autowired
    WorkerRepository workerRepository;

    @Autowired
    WorkService workService;
@Autowired
    FlakeIdGenerator flakeIdGenerator ;

    public List<TaskInfoDto> getTaskInfoDtos(long ownerNo){
        List<TaskInfoEntity> taskInfoEntities = taskInfoRepository.findByOwnerNo(ownerNo);
       return taskInfoEntities.stream()
               .map(t->{
                   List<WorkerEntity> workerEntitiesByOwnerNo = workerRepository.findWorkerEntitiesByOwnerNo(t.getTaskno());
                   List<WorkerDto> workerDtos = workerEntitiesByOwnerNo.stream()

                           .map(workerEntity -> WorkerMapper.toWorkerDto(workerEntity))
                           .collect(Collectors.toList());
                    return TaskInfoMapper.toTaskInfoDto(t, workerDtos);
               }).collect(Collectors.toList());

    }
    @Transactional
    public List<TaskInfoDto> saveTaskInfoDto(long ownerNo,TaskInfoDto taskInfoDto){
        log.info("保存新的任务信息：{},ownerNo：{}",taskInfoDto,ownerNo);
        if (taskInfoDto.getTaskNo() == 0) {
            taskInfoDto.setTaskNo(flakeIdGenerator.newId());
        }
        TaskInfoEntity taskInfoEntity = TaskInfoMapper.toTaskInfoEntity(ownerNo, taskInfoDto);
        List<WorkerDto> workers = taskInfoDto.getWorkers();

        /*workers.stream()
                .filter(Objects::nonNull)
                .forEach(t->{
                    WorkerEntity workerEntity = WorkerMapper.toWorkerEntity(taskInfoDto.getTaskNo(), t);
                    workerRepository.save(workerEntity);
                });*/
        workService.saveWorkerDtos(workers, taskInfoDto.getTaskNo());
        taskInfoRepository.save(taskInfoEntity);
        return getTaskInfoDtos(ownerNo);
    }

    public List<TaskInfoDto> saveTaskInfoDtos(long ownerNo,List<TaskInfoDto> taskInfoDtos){
        taskInfoDtos.stream()
                .forEach(t->saveTaskInfoDto(ownerNo, t));
        return getTaskInfoDtos(ownerNo);
    }

    public void deleteTask(long id){
        taskInfoRepository.deleteById(id);

    }

}
